package com.aorise.controller.cmu;

import com.alibaba.fastjson.JSON;
import com.aorise.model.cmuReq.DevConfigReq;
import com.aorise.model.cmuReq.ReadyUpdateChild;
import com.aorise.service.cmu.CmuService;
import com.aorise.utils.json.JsonResponseData;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


/**
 * @Auther: zhouhao
 * @Date: 2019/7/19
 * @Description:
 */
@Api(description ="CMU自测调用长沙接口")
@Slf4j
@RestController
@RequestMapping("cmu")
public class cmuTest {

    @Autowired
    CmuService cmuService;

    @RequestMapping(value = "/getDev", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getDev(String url,String no) {
        String dev = cmuService.getDev(url, no);
        System.out.println(dev);
        return JsonResponseData.ofSuccess(dev);
    }

    @ApiOperation(value = "设置IMB配置表", httpMethod = "PUT", response = String.class, notes = "设置IMB配置表")
    @RequestMapping(value = "/putDevConfig", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String putDevConfig() {

        DevConfigReq devConfig = new DevConfigReq();
        devConfig.setCMD("PutDevConfig");
        String s = JSON.toJSONString(devConfig);
        String url ="http://222.244.147.121:18090/cmu/putDevConfig";
        String no ="1912031600010002";

        String dev = cmuService.putDev(url, no,s);
        return JsonResponseData.ofSuccess(dev);
    }

    @ApiOperation(value = "控制IMB命令", httpMethod = "PUT", response = String.class, notes = "设置IMB设置IMB时间")
    @RequestMapping(value = "/putDevTime", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String putDevTime() {

        DevConfigReq devConfig = new DevConfigReq();
        devConfig.setCMD("putDevTime");
        String s = JSON.toJSONString(devConfig);
        String url ="http://222.244.147.121:18090/cmu/putDevTime";
        String no ="1912031600010002";

        String dev = cmuService.putDev(url, no,s);
        return JsonResponseData.ofSuccess(dev);
    }

    @ApiOperation(value = "设置IMB设置IMB时间", httpMethod = "PUT", response = String.class, notes = "控制IMB命令")
    @RequestMapping(value = "/putDevControl", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String putDevControl() {

        Map map = Maps.newHashMap();
        map.put("CMD","putDevTime");
        map.put("Date",getCurrentTime("yyyy-mm-dd"));
        map.put("Time",getCurrentTime("HH-mm-ss"));
        String s = JSON.toJSONString(map);
        String url ="http://222.244.147.121:18090/cmu/putDevControl";
        String no ="1912031600010002";

        String dev = cmuService.putDev(url, no,s);
        return JsonResponseData.ofSuccess(dev);
    }

    /**
     * 获取当前时间
     * @return 返回当前时间字符串
     */
    private String getCurrentTime(String format){
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date()).toString();
    }

    @ApiOperation(value = "请求远程升级IMB固件程序", httpMethod = "POST", response = String.class, notes = "请求远程升级IMB固件程序")
    @RequestMapping(value = "/postReadyUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String postReadyUpdate() {

        ReadyUpdateChild updateChild = new ReadyUpdateChild();
        updateChild.setCMD("UpdateReq");
        updateChild.setVersion("V01.00");
        String s = JSON.toJSONString(updateChild);

        String url ="http://222.244.147.121:18090/cmu/putDevControl";
        String no ="1912031600010002";
        String s3 = cmuService.postDev(url, no, s);
        return JsonResponseData.ofSuccess(s3);
    }

    @ApiOperation(value = "升级后执行开始远程升级命令", httpMethod = "POST", response = String.class, notes = "升级后执行开始远程升级命令")
    @RequestMapping(value = "/postUpdate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String postUpdate() {

        Map map = Maps.newHashMap();
        map.put("CMD","UpdateStr");
        map.put("PackNo",0);
        map.put("PackSize",256);
        map.put("Status",0);
        map.put("Data","MTIzNDU2Nzg5MDEyMzQ1Njc4OTA5ODc2NTQ1Njc4OTA5ODc2NTY3ODkwOTg3NjVydDRyNGZnaGI0MzIxMmRjeA==");
        map.put("CRC32",0xC8CE42B3);
        String s = JSON.toJSONString(map);

        String url ="http://222.244.147.121:18090/cmu/postUpdate";
        String no ="1912031600010002";
        String s3 = cmuService.postDev(url, no, s);
        return JsonResponseData.ofSuccess(s3);
    }
}
